 /*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://www.opendomo.com
// date: APRIL 2009
//
// CHANGES:
//
// ----------------------------------------------------------------------------


#ifndef __QUEUE_H__
#define __QUEUE_H__


#include <stdio.h>
#include <stdlib.h>


#define QUEUE_CREATE(varname, type) \
   typedef struct _##varname##_node \
   { \
      type value; \
      struct _##varname##_node *next; \
   } \
   varname##_node; \
   \
   typedef struct _##varname##_queue \
   { \
      size_t size; \
      varname##_node *head; \
      varname##_node *tail; \
   } \
   varname##_queue; \
   \
   varname##_queue *varname = malloc(sizeof(varname##_queue)); \
   varname->size = 0; \
   varname->head = NULL; \
   varname->tail = NULL; 


#define QUEUE_PTR(varname, type) \
   typedef struct _##varname##_node \
   { \
      type value; \
      struct _##varname##_node *next; \
   } \
   varname##_node; \
   \
   typedef struct _##varname##_queue \
   { \
      size_t size; \
      varname##_node *head; \
      varname##_node *tail; \
   } \
   varname##_queue; \
   \
   varname##_queue *varname = NULL;


#define QUEUE_ENQUEUE(varname, newvalue) \
   { \
      varname##_node *new_node = NULL; \
      varname##_node *ptr = varname->tail; \
      new_node = malloc(sizeof(varname##_node)); \
      new_node->value = newvalue; \
      new_node->next = NULL; \
      varname->tail = new_node; \
      varname->size++; \
      if(ptr) ptr->next = new_node; \
      if(!varname->head) varname->head = varname->tail; \
   }


#define QUEUE_DEQUEUE(varname) \
   { \
      if(varname->size>0) \
      { \
         varname->size--; \
         varname##_node *head = varname->head; \
         varname->head = varname->head->next; \
         free(head); \
         \
         if(varname->size==0) \
         { \
            varname->head = NULL;\
            varname->tail = NULL;\
         } \
      } \
   }

 

#define QUEUE_SIZE(varname)  varname->size

#define QUEUE_GET_HEAD(varname) (varname->size>0 ? varname->head->value : 0)

#define QUEUE_GET_TAIL(varname) (varname->size>0 ? varname->tail->value : 0)


#define QUEUE_DESTROY(varname) \
   { \
      varname##_node *prev = NULL; \
      varname##_node *it = NULL; \
      for(it=varname->head; it!=NULL; it=it->next) \
      { \
         if(prev) free(prev); \
         prev = it; \
      } \
      if(prev) free(prev); \
      if(varname) free(varname); \
   }

#endif


